package com.location.footprint.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

import com.location.footprint.R;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.scwang.smart.refresh.layout.util.SmartUtil;

import net.frakbot.jumpingbeans.JumpingBeans;

public class ClassicsHeader extends LinearLayout implements RefreshHeader {

//    private TextView mHeaderText;//标题文本
//    private ImageView mProgressView;//刷新动画视图


    public ClassicsHeader(Context context) {
        super(context);
        initView(context);
    }

    public ClassicsHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initView(context);
    }

    public ClassicsHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initView(context);
    }

    private void initView(Context context) {
        setGravity(Gravity.CENTER);
        setOrientation(VERTICAL);
        setBackgroundColor(context.getResources().getColor(R.color.gray));
        TextView text_title = new TextView(context);
        text_title.setGravity(Gravity.CENTER|Gravity.BOTTOM);
        text_title.setText("我曾经跨过山河大海...");
        text_title.setTextColor(context.getResources().getColor(R.color.white));
        JumpingBeans.with(text_title).makeTextJump(0,text_title.length()).build();
        TextView mHeaderText = new TextView(context);
        mHeaderText.setText("Copyright @2019 Microtrace");
        mHeaderText.setTextColor(context.getResources().getColor(R.color.white));
        mHeaderText.setGravity(Gravity.CENTER);

        addView(text_title, LayoutParams.WRAP_CONTENT, SmartUtil.dp2px(30));
        addView(mHeaderText, LayoutParams.WRAP_CONTENT, SmartUtil.dp2px(40));
//        setMinimumHeight(SmartUtil.dp2px(60));
//        View view = LayoutInflater.from(context).inflate(R.layout.layout_footprint_header, null);
//        TextView text_title = view.findViewById(R.id.text_title);
//        JumpingBeans.with(text_title).makeTextJump(0,text_title.length()).build();
//        addView(view);
    }

    public View getView() {
        return this;//真实的视图就是自己，不能返回null
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;//指定为平移，不能null
    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int headHeight, int maxDragHeight) {

    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {

        if (success) {
//            mHeaderText.setText("刷新完成");
        } else {
//            mHeaderText.setText("刷新失败");
        }
        return 100;//延迟500毫秒之后再弹回
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState) {
            case None:
            case PullDownToRefresh:
//                mHeaderText.setText("下拉开始刷新");

//                mProgressView.setVisibility(GONE);//隐藏动画
//                mArrowView.animate().rotation(0);//还原箭头方向
                break;
            case Refreshing:
//                mHeaderText.setText("正在刷新");
//                mProgressView.setVisibility(VISIBLE);//显示加载动画
//                mArrowView.setVisibility(GONE);//隐藏箭头
                break;
            case ReleaseToRefresh:
//                mHeaderText.setText("释放立即刷新");
//                mArrowView.animate().rotation(180);//显示箭头改为朝上
                break;
        }
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int maxDragHeight) {
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
    }

    @Override
    public void setPrimaryColors(@ColorInt int... colors) {
    }
}
